import {$get, $post} from '../utils/request';
import {$msg_s, $msg_e} from '../utils/msg';

//获取房间类型列表
export const getRoomTypeList = () => {
  let data = $get('RoomType/list');

  data = [
    {
      roomTypeId: '1',
      roomTypeName: '单人间',
      roomTypePrice: 200,
      bedNum: 1,
      typeDescription: '<h1>单人间房型描述</h1>',
    },
    {
      roomTypeId: '2',
      roomTypeName: '双人间',
      roomTypePrice: 300,
      bedNum: 2,
    },
    {
      roomTypeId: '3',
      roomTypeName: '三人间',
      roomTypePrice: 300,
      bedNum: 3,
    },
  ];

  return data;
}

//验证添加入参
const checkInput = (params) => {
  if(!params.roomTypeName){
    $msg_e('房间类型名称必填');
  }else if(!params.roomTypePrice){
    $msg_e('房间价格必填');
  }else if(isNaN(params.roomTypePrice)){
    $msg_e('房间价格必须是数字');
  }else if(!params.bedNum){
    $msg_e('房间床位数必填');
  }else if(!Number.isInteger(parseInt(params.bedNum))){
    $msg_e('房间床位数必须是整数');
  }else{
    //入参正确
    return true;
  }

  return false;
}

//添加房间类型
export const add = (params) => {
  if(!checkInput(params)){
    //入参错误
    return false;
  }

  let res = $post('RoomType/Add', params);

  res = {
    success: true,
    message: '添加房间类型成功',
  }

  if(res.success){
    //成功
    $msg_s(res.message);
  }else{
    //失败
    $msg_e(res.message);
  }

  return res.success;
}

//编辑房间类型
export const update = (params) => {
  if(!checkInput(params)){
    //入参错误
    return false;
  }

  let res = $post('RoomType/Update', params);

  res = {
    success: true,
    message: '编辑房间类型成功',
  }

  if(res.success){
    //成功
    $msg_s(res.message);
  }else{
    //失败
    $msg_e(res.message);
  }

  return res.success;
}

//删除房型
export const del = (params) => {
  let data = $post('RoomType/Delete', params);

  data = {
    success: true,
    message: '删除房型成功',
  }

  const {success, message} = data;

  if(success){
    //成功
    $msg_s(message);
  }else{
    //失败
    $msg_e(message);
  }

  return success;
}

//获取各房间类型的销售额
export const getTotalTypePrice = async () => {
  let data = await $get('RoomType/TotalTypePrice');

  /* data = [
    {
      roomTypeName: '单人间',
      totalMoney: 600,
    },
  ]; */

  return data;
}